﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
 
using WorkBasket.Data.Entities;
using WorkBasket.Data.Repository;
using WorkBasket.Infrastructure;
using WorkBasket.Models;
using System.Data.SqlClient;
 


namespace WorkBasket.Controllers.Api
{
    [Authorize]
    public class StepController : ApiController
    {

        private SOC soc = new SOC(new MyContext());
        [Route("api/Step/GetByWorkflow/{ID}")]
        [HttpGet]
        // GET: Step
        public IHttpActionResult Index()
        {
            return Ok();
        }
        [HttpPost]
        [Route("api/Step/save")]
        public IHttpActionResult Post([FromBody] Step value)
        {
            try
            {
                

                if (value.StepID == 0)
                {
                    soc.Steps.Add(value);
                }
                else
                {
                    soc.Steps.Update(value);
                }

                soc.CommitChanges();
                return Json(new DataReturn("Step Saved", value));
            }
            catch (Exception e)
            {
                return new System.Web.Http.Results.ExceptionResult(e, this);
            }
        }
        [Route("api/Step/GetByKey/{ID}")]
        public IHttpActionResult GetByKey(int id)
        {
            return Json(soc.Steps.GetByID(id));
     
        }

        [HttpPost]
        [Route("api/step/delete")]
        public IHttpActionResult Delete([FromBody] Step value)
        {
            try
            {
                soc.Steps.Delete(value.StepID);
                soc.CommitChanges();
                return Json(new DataReturn("Step Deleted", value));
            }
            catch (Exception e)
            {
                return new System.Web.Http.Results.ExceptionResult(e, this);
            }
        }
    }
}